<?php
namespace app\admin\controller;

use think\Db;
use app\common\controller\AdminBase;
use app\common\model\Reserved as ReservedModel;
use app\common\model\Nav as NavModel;
use think\Session;

//1证件办理,2保险,3金融,4油卡/ETC,5挂靠
class Bkxm extends AdminBase
{
	protected $reserved_model;
	protected $nav_model;

	protected function _initialize()
	{
		parent::_initialize();
		$this->reserved_model = new ReservedModel();
		$this->nav_model = new NavModel();

		$nav_list = $this->nav_model->where('status',1)->column('name','id');

		$this->assign('nav_list',$nav_list);
	}

	public function index($keyword = '', $page = 1)
	{
		$map = [];
		if($keyword){
			$map['reservedName|reservedPhone'] = ['like',"%{$keyword}%"];
		}

		$reserved_list = $this->reserved_model->where($map)->where('classifyID',2)->order('id DESC')->paginate(15, false, ['page' => $page]);

		return $this->fetch('index', ['reserved_list' => $reserved_list, 'keyword' => $keyword]);
	}

	public function reading($id)
	{
		$reserved = $this->reserved_model->find($id);
		return $this->fetch('reading',['reserved' => $reserved]);
	}

	public function update($id)
	{
		$reserved = $this->reserved_model->find($id);
		$reserved->contact = '1';
		$reserved->contactDate = date('Y-m-d H:i:s',time());
		$reserved->contactMan = Session::get('admin_name');
		if($reserved->save()){
			return $this->success('更新成功');
		}else{
			return $this->error('更新失败');
		}
	}

	public function delete($id = 0, $ids = [])
	{
		$id = $ids ? array_merge($ids) : $id;

		if($ids){
            $i = 0;
            for(;$i<count($ids);$i++){
                $fileImg = $this->reserved_model->where('id',$ids[$i])->value('photo');
                $ji = 0;
                for(;$ji<count($fileImg);$ji++){
                    if($fileImg[$ji]){
                       if(file_exists('./'.$fileImg[$ji])){
                           unlink('./'.$fileImg[$ji]);
                       }
                    }
                }

            }
        }

		if($id){
			if($this->reserved_model->destroy($id)){
				return $this->success('删除成功');
			}else{
				return $this->error('删除失败');
			}
		}else{
			return $this->error('请选择需要删除的内容');
		}
	}
}